var util = require("./../util/util");
var jwt = require('jsonwebtoken');
var config = require('./../config/config')

/**
 * 用户地址类(用户:地址=1:n)
 */
module.exports = function(sequelize, DataTypes) {
    let address = sequelize.define("address", {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false, //非空
            autoIncrement: true, //自动递增
            primaryKey: true //主键
        },
        userId: {
            type: DataTypes.INTEGER,
            field: "user_id",
            comment: "用户id",
            references: { //引用user模型里的id属性，即在loginInfo表中添加user_id逻辑关联
                model: "user",
                key: "id"
            }
        },
        nation: {
            type: DataTypes.STRING,
            allowNull: true,
            comment: "国家"
        },
        province: {
            type: DataTypes.STRING,
            allowNull: true,
            comment: "省份"
        },
        city: {
            type: DataTypes.STRING,
            allowNull: true,
            comment: "城市"
        },
        district: {
            type: DataTypes.STRING,
            allowNull: true,
            comment: "区域"
        },
        addr: {
            type: DataTypes.STRING,
            allowNull: true,
            comment: "详细地址"
        },
        adcode: {
            type: DataTypes.STRING,
            field: "zip_code",
            allowNull: true,
            comment: "邮编"
        },
        trace: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: "追踪姓名"
        },
        ismine:{
            type: DataTypes.INTEGER,
            allowNull: false,
            field: "is_mine",
            comment: "是否是本人"
            //0是他人，1是自己
        },
        type:{
            type: DataTypes.STRING,
            allowNull: true,
            comment: "iptype"
        }
    }, {
        underscored: true, //额外字段以下划线来分割
        timestamps: true, //取消默认生成的createdAt、updatedAt字段
        freezeTableName: true, // Model 对应的表名将与model名相同
        //静态方法，即user模型自带的方法
        // charset: 'utf8',   //手动指定编码
        // collate: 'utf8_general_ci',
        comment: "用户地址表",
        indexes: [{
            name: "address_userId",
            method: "BTREE",
            fields: ["user_id"]
        }]
    });

    address.createAddress = function (userId, param, callback) {
        // Users exists with same email check
            if(userId){
                this.create({
                    userId: userId,
                    nation: param.nation,
                    city:param.city,
                    province:param.province,
                    district:param.district,
                    addr:param.addr,
                    adcode:param.adcode,
                    trace:param.trace,
                    ismine:param.isMine,
                    type:param.type
                }).then(result => {
                    if(result){
                        console.log('上传成功')
                        callback(util.successResponse('上传成功'))
                    }else {
                        callback(util.errorResponse('写入失败'))
                    }
                });

            }else {
                callback(util.errorResponse('参数上传错误'))
            }
    }

    address.getAddressList = function (userId, callback) {
        // Users exists with same email check
        console.log('用户id'+ userId)
        if(userId){
            this.findAll({ where: {userId: userId} }).then(objects => {
                if(objects){
                    callback(util.successResponse('成功',{objects}))
                }else {
                    callback(util.errorResponse('没有找到该用户的追踪信息'))
                }
            }).catch(reason => {
                console.log(reason)
            })

        }else {
            callback(util.errorResponse('参数上传错误'))
        }
    }



    // address.sync();

    return address
}
